<?php
namespace Peas\Kernel\System\Plugins\Template;

use Peas\Kernel\Helper\TokenHelper;

/**
 * Peas Framework
 *
 * 表单令牌标签扩展
 *
 * @author  Hongzan Zhang <zhanghongzan@163.com>
 * @version $Id$
 */
class TokenTag
{
    /**
     * 生成token表单标签
     *
     * @param  Peas\View\Compiler\Compiler $compiler
     * @param  string $tagStr
     * @return string
     */
    public function begin(&$compiler, $tagStr)
    {
        $createCode = $tokenName = '';
        if (empty($tagStr)) {
            $createCode = '\Peas\Kernel\Helper\TokenHelper::autoCreate()';
            $tokenName = TokenHelper::$defaultName;
        } else {
            $createCode ='\Peas\Kernel\Helper\TokenHelper::create(\'' . $tagStr . '\')';
            $tokenName = $tagStr;
        }
        return '<input type="hidden" name="' . $tokenName . '" value="<?php echo ' . $createCode . ';?>" />';
    }
}
